package com.ysd.lis.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.WkHoliday;
import com.ysd.lis.entity.WkKnowledge;
import com.ysd.lis.mapper.WkHolidayMapper;
import com.ysd.lis.service.WkHolidayService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 节假日表 服务实现类
 * </p>
 *
 * @author LiuQi
 * @since 2024-03-19
 */
@Service
public class WkHolidayServiceImpl extends ServiceImpl<WkHolidayMapper, WkHoliday> implements WkHolidayService {

    @Autowired
    WkHolidayMapper wkHolidayMapper;

    @Override
    public Result getHolidayList(String keyWord, String year) {
        LambdaQueryWrapper<WkHoliday> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(WkHoliday::getDelFlag, 0)
                .and(StrUtil.isNotEmpty(keyWord), wrapper -> wrapper.like(WkHoliday::getHolidayCode, keyWord).or().like(WkHoliday::getHolidayName, keyWord));
        if (StrUtil.isNotEmpty(year)) {
            DateTime yyyy = DateUtil.parse(year, "yyyy");
            lambdaQueryWrapper.between(WkHoliday::getHoliday, DateUtil.beginOfYear(yyyy), DateUtil.endOfYear(yyyy));
        }
        List<WkHoliday> wkHolidays = wkHolidayMapper.selectList(lambdaQueryWrapper);
        return Result.succ(1, "查询成功！", wkHolidays);
    }

    @Override
    public Result addHoliday(WkHoliday wkHoliday) {
        boolean b = this.save(wkHoliday);
        if (b) {
            return Result.succ(1, "添加成功！", b);
        } else {
            return Result.fail("添加失败！");
        }
    }

    @Override
    public Result updateHoliday(WkHoliday wkHoliday) {
        boolean b = this.updateById(wkHoliday);
        if (b) {
            return Result.succ(1, "编辑成功！", b);
        } else {
            return Result.fail("编辑失败！");
        }
    }

    @Override
    public Result deleteHolidayByIds(List<String> ids) {
        boolean b = this.removeByIds(ids);
        if (b) {
            return Result.succ(1, "批量删除成功！", b);
        } else {
            return Result.fail("批量删除失败！");
        }
    }

}
